package state;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;

public class Image {
  private javax.microedition.lcdui.Image image;
  
  private String name = "";
  
  private String category = "";
  
  private boolean isStatic = false;
  
  private Vector listeners = null;
  
  private static Random random = new Random(1000);
  
  private long lastTimeUse = System.currentTimeMillis();
  
  protected Image(String category, String name) {
    this.name = name;
    this.category = category;
  }
  
  protected Image(javax.microedition.lcdui.Image image) {
    this.image = image;
    this.name = String.valueOf(System.currentTimeMillis()) + random.nextLong();
  }
  
  public Image(javax.microedition.lcdui.Image image, String category, String name) {
    this.image = image;
    this.name = name;
    this.category = category;
  }
  
  
  public void addUpdateImageListener(LoadResourceCompleteListener listener) {
    if (listeners == null) {
      listeners = new Vector();
    }
    listeners.addElement(listener);
  }
  
  public Image setStatic(boolean isStatic) {
    this.isStatic = isStatic;
    return this;
  }
  
  public boolean isStatic() {
    return isStatic;
  }

  public String getName() {
    return name;
  }
  
  public void setName(String name) {
    this.name = name;
  }
  
  public void setCategory(String category) {
    this.category = category;
  }
  
  public String getCategory() {
    return category;
  }
  
  public void setLastTimeUse(long lastTimeUse) {
    this.lastTimeUse = lastTimeUse;
  }
  
  public long getLastTimeUse() {
    return lastTimeUse;
  }
  
  public javax.microedition.lcdui.Image getImage() {
    return image;
  }
  
  public void setImage(javax.microedition.lcdui.Image image) {
    this.image = image;
    if (listeners != null) {
      for (int i = 0; i < listeners.size(); i++) {
        LoadResourceCompleteListener listener = (LoadResourceCompleteListener) listeners.elementAt(i);
        listener.onLoadResourceDone(this);
      }
    }
  }
  
  public int getWidth() {
    if (image == null) {
      return 0;
    }
    return image.getWidth();
  }

  public static Image createImage(String name) throws IOException {
    Image image = ResourceManager.createEmptyImage();
    image.setImage(javax.microedition.lcdui.Image.createImage(name));
    return image;
  }
  
  public static Image createImage(byte[] data, int offset, int size) {
    Image image = ResourceManager.createEmptyImage();
    image.setImage(javax.microedition.lcdui.Image.createImage(data, offset, size));
    return image;
  }

  public static Image createRGBImage(int[] rgb, int width, int height, boolean processAlpha) {
    Image image = ResourceManager.createEmptyImage();
    image.setImage(javax.microedition.lcdui.Image.createRGBImage(rgb, width, height, processAlpha));
    return image;
  }
  
  public static Image createImage(int width, int height) {
    Image image = ResourceManager.createEmptyImage();
    image.setImage(javax.microedition.lcdui.Image.createImage(width, height));
    return image;
  }
  
  public static Image createImage(Image image, int x, int y, int width, int height, int rotate) {
    if (image.getImage() == null) {
      Image emptyImage = ResourceManager.createEmptyImage();
      LoadResourceCompleteListener listener = new LoadResourceCompleteListener(image.getName()) {
        public void onLoadResourceDone(Image image) {
          Image targetImage = ResourceManager.getImage(ResourceManager.EMPTY_CATEGORY, getString("imageName"));
          Image tempImage = createImage(image, getInt("x"), getInt("y"), getInt("width"), getInt("height"), getInt("rotate"));
          targetImage.setImage(tempImage.getImage());
        }
      }.put("imageName", emptyImage.getName()).put("x", x).put("y", y).put("width", width).put("height", height).put("rotate", rotate);
      ResourceManager.addLoadImageCompleteListener(listener);
      return emptyImage;
    }
    return new Image(javax.microedition.lcdui.Image.createImage(image.getImage(), x, y, width, height, rotate));
  }

  public static Image createImage(ByteArrayInputStream byteArrayInputStream) throws IOException {
    Image image = ResourceManager.createEmptyImage();
    image.setImage(javax.microedition.lcdui.Image.createImage(byteArrayInputStream));
    return image;
  }
  
  public int getHeight() {
    if (image == null) {
      return 0;
    }
    return image.getHeight();
  }
  
  public void getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height) {
    if (image != null) {
      image.getRGB(rgbData, offset, scanlength, x, y, width, height);
    }
  }

  public Graphics getGraphics() {
    if (image == null) {
      return null;
    }
    return new Graphics(image.getGraphics());
  }
  
  public static void detroyImage(Image image) {
    if (image != null) {
      image.setStatic(false);
      ResourceManager.removeImage(image.getCategory(), image.getName());
      image.setImage(null);
    }
  }
  
  public String toString() {
    return "[" + getName() + "," + getWidth() + "," + getHeight() + "]";
  }
}
